<template>
	<view>
		<div class="index">
			<!-- 轮播图开始 -->
			<div class="swiper">
				<swiper class="swiper-container" indicator-dots="true" autoplay="true" interval="3000" circular="true" duration="500">
					<block v-for="(item, index) in banner " :key="index">
						<!-- #ifdef MP -->
						<swiper-item class="swiper-item">
							<!-- #endif -->
							<!-- #ifdef APP-PLUS -->
							<swiper-item  class="swiper-item">
								<!-- #endif -->
								<!-- #ifdef H5 -->
								<swiper-item  class="swiper-item">
									<!-- #endif -->
									<image :src="fileServerHost + item.url" class="slide-image" />
								</swiper-item>
							</swiper-item>
						</swiper-item>
					</block>
				</swiper>
			</div>
			<!-- 轮播图结束 -->

			<!-- 资讯开始 -->
			<div class="list" v-for="(item,index) in list" link :key="index">
				<!-- 图文卡片模式 -->
				<div v-if="item.image!=null" >
					<uni-card
					    mode="style"
						:is-shadow="true"
						:thumbnail="item.image ? fileServerHost + item.image:''"
						@click="onGotoDetails(item)"
						>
						<div style="text-align: left;">
							{{item.title}}
						</div>
						<div style="text-align: right;padding-top: 10px;color:#808080;">
							<uni-dateformat :date="item.updateTime" format="yyyy/MM/dd"></uni-dateformat>
						</div>
					</uni-card>
				</div>
				<div v-else>
					<uni-card
					    mode="basic"
						:is-shadow="true"
						@click="onGotoDetails(item)"
						>
						<!-- <span style="color: #999;font-size: 28rpx;">详细 ></span> -->
						<div style="text-align: left;">
							{{item.title}}
						</div>
						<div style="text-align: right;margin-top: 10rpx;color:#808080;">
							<uni-dateformat :date="item.updateTime" format="yyyy/MM/dd"></uni-dateformat>
						</div>
					</uni-card>
				</div>
			</div>
			<!-- 资讯结束 -->
		</div>
	</view>
</template>

<script>
	import {
		navApi,
		infoApi,
		logincommonAccountApiEncode
	} from "@/server/config.js";
	import {
		toLogin,
		fileServerHost,
		post,
		get
	} from "@/server/api.js";
	import Jfooter from "@/components/footer/footer.vue";
	import showOnline from "@/components/show/showOnLine.vue";
	const indexData = require("@/static/mockjs/index.js");
	export default {
		async onShow() {
			await this.processOpenid();
			await this.judgeLogin();
			this.getData();
			this.getInfoList();
			this.fileServerHost = fileServerHost
			this.logo = "./static/images/ysh.jpeg";
		},
		computed: {},
		onLoad(options) {
			this.wxCode = options.code
			//APP引导页
			// #ifdef APP-PLUS
			this.checkGuide();
			// #endif
			let ua = navigator.userAgent.toLowerCase();
			//如果是微信浏览器就显示微信登楼
			if (ua.search(/micromessenger/i) > -1) {
				this.isWeixin = true;
			} else {
				this.isWeixin = false;
			}
		},
		//滚动奥底部，判断是否还有资讯
		onReachBottom() {
			this.pageNo = this.pageNo + 1;
			if (this.pageNo > this.total) {
				return false;
			}
			this.getInfoList();
		},

		data() {
			return {
				isClose: false, //顶部新人有礼
				isMaskOpen: true, //遮罩
				close: false, //新手有礼页面
				banner: [],
				list:[],
				pageNo:1,
				total:0,
				logo: "",
				fileServerHost:'',
				wxCode : '',
				isWeixin: false
			};
		},
		components: {
		},
		methods: {
			async processOpenid() {
				let me = this;
				if (this.isWeixin && this.wxCode) {
					let openId = uni.getStorageSync('openid');
					await get('/erp/openid/find', {openid:openId}).then(function(res) {
						if (!res.data.records[0]) {
							openId = '';
						}
					})
					if (!openId) {
						let userInfo = uni.getStorageSync('userInfo');
						await post('/app/weixin/openid',
							{code:this.wxCode,userid:userInfo.id,username:userInfo.name,phonenum:userInfo.phoneNumber}).then(function(res) {
								uni.setStorageSync("openid", res.data.openid);
								me.wxCode = ''
						});
					}
				}
			},
			async judgeLogin() {
				let accessToken = uni.getStorageSync('accesstoken');
				let userInfo = uni.getStorageSync('userInfo');
				let openId = uni.getStorageSync('openid');
				if (openId) {
					await get('/erp/openid/find', {openid:openId}).then(function(res) {
						if (!res.data.records[0]) {
							uni.navigateTo({
								url: "/apiPam/logincommon/main"
							});
						}
					})
				}
				if (!userInfo) {
					uni.navigateTo({
						url: "/apiPam/logincommon/main"
					});
				} else {
					//获取轮播图
					const {
						data,
						statusCode
					} = await logincommonAccountApiEncode({
						username: userInfo.phoneNumber,
						password: userInfo.password
					})
					if (statusCode == 200) {
						uni.setStorageSync("accesstoken", data.access_token);
					} else {
						uni.navigateTo({
							url: "/apiPam/logincommon/main"
						});
					}
				}
			},
			async upstarbuy() {},
			bannerLink(url) {
				let http = /^http:\/\/.*/i.test(url);
				let https = /^https:\/\/.*/i.test(url);
				if (!http && !https) {
					uni.navigateTo({
						url: "/apiShop/goods/main?id=" + url
					});
				} else {
					if (url.length < 9) {
						console.log("这不是一个链接");
						return false;
					}
					// #ifdef APP-PLUS
					plus.runtime.openURL(url);
					// #endif
					// #ifdef H5
					location.href = url;
					// #endif
				}
			},
			async getData() {
				//获取轮播图
				let res = await navApi({
					title:'',
					pageNo:1,
					pageSize:3
				})
				
				//轮播图
				this.banner = res.data.records
			},
			
			async getInfoList(){
				let res = await infoApi({
					title:'',
					pageNo:this.pageNo,
					pageSize:20
				})
				this.list = res.data.records
				this.total = res.data.totalPage
			},
			onGotoDetails(item) {
				console.info(item)
				uni.navigateTo({
					url: "/apiPam/info/main?id=" + item.id
				});
			},
			async mpvueScanCode() {
				// #ifdef MP-ALIPAY
				//支付宝扫码
				uni.scan({
					type: "qr",
					success: async res => {
						// my.alert({ title: res.code });
						// console.log(res.code);
						// const data = await scancodeSaveApi({
						//   code: res.code
						// });
						// if (data.id) {
						//   uni.navigateTo({
						//     url: "/apiShop/goods/main?id=" + data.id
						//   });
						// } else {
						uni.showToast({
							title: "测试扫码效果", //提示的内容,
							duration: 2000, //延迟时间,
							icon: "none",
							mask: true, //显示透明蒙层，防止触摸穿透,
							success: res => {}
						});
						// }
					}
				});
				// #endif
				// #ifdef MP-WEIXIN
				//微信扫码
				// 只允许从相机扫码
				uni.scanCode({
					onlyFromCamera: true,
					success: async res => {
						// console.log(res)
						// const data = await scancodeSaveApi({
						//   code: res.result
						// });
						// if (data.id) {
						//   uni.navigateTo({
						//     url: "/apiShop/goods/main?id=" + data.id
						//   });
						// } else {
						uni.showToast({
							title: "测试扫码效果", //提示的内容,
							duration: 2000, //延迟时间,
							icon: "none",
							mask: true, //显示透明蒙层，防止触摸穿透,
							success: res => {}
						});
						// }
					}
				});
				// #endif
				// #ifdef APP-PLUS
				// 允许从相机和相册扫码
				uni.scanCode({
					success: function(res) {
						uni.showToast({
							title: "测试扫码效果", //提示的内容,
							duration: 2000, //延迟时间,
							icon: "none",
							mask: true, //显示透明蒙层，防止触摸穿透,
							success: res => {}
						});
						console.log("条码类型：" + res.scanType);
						console.log("条码内容：" + res.result);
					}
				});

				// 只允许通过相机扫码
				// uni.scanCode({
				//     onlyFromCamera: true,
				//     success: function (res) {
				//         console.log('条码类型：' + res.scanType);
				//         console.log('条码内容：' + res.result);
				//     }
				// });
				// #endif
			},
			checkGuide() {
				// 思路： 检测是否有启动缓存，如果没有，就是第一次启动，第一次启动就去 启动介绍页面
				const launchFlag = uni.getStorageSync("launchFlag");
				console.log(launchFlag);
				if (launchFlag) {
					uni.switchTab({
						url: "/pages/index/main"
					});
				} else {
					console.log("去登陆页面");
					uni.navigateTo({
						url: "/apiUtil/guide/main"
					});
				}
			},
			/**
			 * 统一跳转接口,拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			navTo(url) {
				uni.navigateTo({
					url
				});
			},
			// 关闭新人有礼
			gift_close() {
				this.close = false;
			},
			is_close() {
				this.isClose = false;
			},
			async gift_close_url() {
				if (toLogin() == true) {
					if (this.prompt.prompt_bonus_id > 0) {
						let image_url = this.prompt.prompt_image_url;
						const data = await SendBonusApi({
							bonus_id: this.prompt.prompt_bonus_id
						});
						this.close = false;
						if (data.data.res == false) {
							uni.showToast({
								title: data.data.msg,
								icon: "none",
								duration: 1000,
								success: function() {
									setTimeout(function() {
										uni.switchTab({
											url: image_url,
											fail: function() {
												uni.navigateTo({
													url: image_url
												});
											}
										});
									}, 1000);
								}
							});
						} else {
							uni.showToast({
								title: data.data.msg,
								icon: "none",
								duration: 1000,
								success: function() {
									setTimeout(function() {
										uni.switchTab({
											url: image_url,
											fail: function() {
												uni.navigateTo({
													url: image_url
												});
											}
										});
									}, 1000);
								}
							});
						}
					} else {
						let image_url = this.prompt.prompt_image_url;
						uni.switchTab({
							url: this.prompt.prompt_image_url,
							fail: function() {
								uni.navigateTo({
									url: image_url
								});
							}
						});
					}
				}
			}
		},
		created() {}
	};
</script>

<style lang='scss' scoped>
	@import "./style.scss";
</style>
